package ee.ttu.serge;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileHelper {

   public static List<Integer> read(String path) throws IOException {
      FileInputStream fstream = new FileInputStream(path);
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      List<Integer> list = new ArrayList<Integer>();
      while ((strLine = br.readLine()) != null) {
         list.add(Integer.valueOf(strLine));
      }
      in.close();
      return list;
   }
   
   public static void write(String path, List<Integer> list) throws IOException {
      FileWriter fstream = new FileWriter(path);
      BufferedWriter out = new BufferedWriter(fstream);
      for (Integer i : list) {
         out.write(String.valueOf(i) + "\n");
      }
      out.close();
   }
   
}
